﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AudioCore.Biz;
using AudioCore.Entity;
using AudioCore.Common;
using AudioBook.AudioManagement.Util;

namespace AudioBook.AudioManagement.AudioManage
{
    public partial class AddArticle : System.Web.UI.Page
    {
        #region Fields & constants

        ArticleBiz bizArticle = new ArticleBiz();
        CategoryBiz bizCategory = new CategoryBiz();
        CategoryArticleBiz bizCategoryArticle = new CategoryArticleBiz();

        #endregion Fields & constants

        #region Private methods

        private void LoadData()
        {
        }

        private int SaveArticle()
        {
            ArticleEntity enArticle = new ArticleEntity();
            enArticle.ArticleName = txtArticleName.Text;
            enArticle.Description = txtDescription.Text;
            enArticle.HasImage = fulImage.HasFile;
            enArticle.Artist = txtArtist.Text;
            enArticle.Author = txtAuthor.Text;
            enArticle.PlayCounter = 0;
            enArticle.IsHot = false;
            enArticle.LastTimeUpdate = DateTime.Now;
            enArticle.CreatedDateTime = DateTime.Now;
            enArticle.RatePoint = int.Parse(dropRate.SelectedValue);
            enArticle.Actived = chkActive.Checked;
            enArticle.Deleted = false;

            int result = bizArticle.AddArticle(enArticle);

            if (result != 0)
            {
                if (fulImage.HasFile)
                {
                    string audioFolder = GlobalParam.DataFolder + Constant.AUDIO_FOLDER + result.ToString() + "\\";
                    if (!Directory.Exists(audioFolder))
                    {
                        Directory.CreateDirectory(audioFolder);
                    }
                    string imgFile = audioFolder + result.ToString() + Constant.IMAGE_EXTENSION;
                    fulImage.SaveAs(imgFile);
                }
                CategoryArticleEntity enCategoryArticle = new CategoryArticleEntity();
                enCategoryArticle.CategoryID = ucCategory.SelectedCategoryID;
                enCategoryArticle.ArticleID = result;
                enCategoryArticle.SortOrder = DateTime.Now;
                bizCategoryArticle.AddCategoryArticle(enCategoryArticle);
            }
            else
            {
                valCommon.IsValid = false;
                valCommon.ErrorMessage = GUIConstants.ErrorMessage.TransactionError;
                return 0;
            }
            return result;
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void btnSaveAndClose_Click(object sender, EventArgs e)
        {
            int result = SaveArticle();
            if (result > 0)
            {
                Response.Redirect(string.Format(GUIConstants.WebPage.AudioPage.DefaultAudio, result));
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int result = SaveArticle();
            if (result > 0)
            {
                txtArticleName.Text = string.Empty;
                txtDescription.Text = string.Empty;
                txtTag.Text = string.Empty;
                chkActive.Checked = true;
            }
        }
    }
}
